package com.cande.loki.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Threads(1) //启动几个线程程来执行这个任务
@Fork(value = 1)  // 启动几个进程来执行这个任务  , jvmArgsAppend = {"-Xmx2048m", "-server", "-XX:+AggressiveOpts"}
@Warmup(iterations = 1)
@State(Scope.Group)
public class TestGroup {

    int x = 0;

    @Benchmark
    @Group("g")
    @GroupThreads(3) // 3个生产者线程
    public void producer() {
        x++;
    }

    @Benchmark
    @Group("g")
    @GroupThreads(1) // 1个消费者线程
    public int consumer() {
        return x--;
    }

    public static void main(String[] args) throws RunnerException {

        Options opt = new OptionsBuilder()
                .include(TestGroup.class.getSimpleName())

                .build();

        new Runner(opt).run();
    }
}
